package com.ssrs.framework.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * @author ssrs
 */
@Component
public class LangUtil {

    private static EightRoesMessageSource messageSource;

    @Autowired
    public LangUtil(EightRoesMessageSource eightRoesMessageSource) {
        LangUtil.messageSource = eightRoesMessageSource;
    }


    /**
     * 重新将数据库中的国际化配置加载
     */
    public static void reload() {
        EightRoesMessageSource.reload();
    }


    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey) {
        try {
            return get(msgKey, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return msgKey;
        }
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey, Object... args) {
        try {
            return get(msgKey, LocaleContextHolder.getLocale(), args);
        } catch (Exception e) {
            return msgKey;
        }
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey, Locale locale, Object... args) {
        try {
            return messageSource.getMessage(msgKey, args, locale);
        } catch (Exception e) {
            return msgKey;
        }
    }
}
